//
//  StarSliderA.m
//  asdfasd
//
//  Created by apple on 16/10/19.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "StarSliderA.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width / 12
@interface StarSliderA ()


@end


@implementation StarSliderA


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if (self) {
        float offsetCenter = WIDTH;
        for (int i = 1; i <= 5; i++) {
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, WIDTH, WIDTH)];
            imageView.image = [UIImage imageNamed:@"liveend_icon_stars_default"];
            imageView.center = CGPointMake(offsetCenter, self.intrinsicContentSize.height / 2.0f);
            offsetCenter += WIDTH * 1.5f;
            
            
            
            [self addSubview:imageView];
            
            
        }
        
    }
    
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:1.0f];
    
    
    return self;

    
}
- (CGSize)intrinsicContentSize{
    
    return CGSizeMake(WIDTH * 8.0f, 34.0f);
    
    
}
- (void)updateValueAtPoint : (CGPoint)point{
    
    int newValue = 0;
    UIImageView *changedView = nil;
    for (UIImageView *eachItem in [self subviews]) {
        if (point.x < eachItem.frame.origin.x) {
            eachItem.image = [UIImage imageNamed:@"liveend_icon_stars_default"];
            
        }
        else{
            
            changedView = eachItem;
            eachItem.image = [UIImage imageNamed:@"liveend_icon_stars_sele"];
            newValue++;
            
        }
    }
    
    if (self.value != newValue) {
        
        self.value = newValue;
        [self sendActionsForControlEvents:UIControlEventValueChanged];
        
        [UIView animateWithDuration:0.15f animations:^{
            changedView.transform = CGAffineTransformMakeScale(1.5f, 1.5f);
            
        } completion:^(BOOL done) {
            [UIView animateWithDuration:0.1f animations:^{
                
                changedView.transform = CGAffineTransformIdentity;
            }];
            
        }];
    }
    
}
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{

    CGPoint touchPoint = [touch locationInView:self];
    [self sendActionsForControlEvents:UIControlEventTouchDown];
    
    [self updateValueAtPoint:touchPoint];
    return YES;




}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{

    CGPoint touchPoint = [touch locationInView:self];
    if (CGRectContainsPoint(self.frame, touchPoint)) {
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    }
    else
        [self sendActionsForControlEvents:UIControlEventTouchUpOutside];
    
    [self updateValueAtPoint:[touch locationInView:self]];
    
    return YES;


}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{

    CGPoint touchPoint = [touch locationInView:self];
    if (CGRectContainsPoint(self.bounds, touchPoint)) {
        [self sendActionsForControlEvents:UIControlEventTouchUpInside];
    }

    else
        [self sendActionsForControlEvents:UIControlEventTouchUpOutside];
    
    

    [self.delegate getNum:self.value];
    

}
- (void)cancelTrackingWithEvent:(UIEvent *)event{

    [self sendActionsForControlEvents:UIControlEventTouchCancel];
 
   

}
@end
